<section class="content-header">
    <h1>
        内容管理
    </h1>
    <ol class="breadcrumb">
        <li><a href="{:U('/Admin/index')}"><i class="fa fa-home"></i>首页</a></li>
        <li class="active">内容管理</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-9">
            <div class="box">
                <div class="box-header">
                    <div class="box-title">
                        <form id="content_newslist" url="{:U('Content/newsList',array('catid'=>$catid))}" onsubmit="return false;">
                            标题:
                            <input type="text" name="search[title]" style="width:130px;padding:2px;height: 20px"/>
                            时 间: <input name="search[begin]" class="easyui-datebox" style="width:100px">
                            至: <input name="search[end]" class="easyui-datebox" style="width:100px">
                            <button onclick="contentNewsListDatagridSearch()" class="btn  btn-info btn-sm"><i
                                    class="fa fa-search"></i>搜索
                            </button>
                        </form>
                    </div>
                    <div class="pull-right">
                        <button id="addContentButton" class="btn btn-block btn-primary btn-sm hide"
                                onclick="contentNewsListAdd()"><i
                                class="fa fa-plus"></i>&nbsp;&nbsp;&nbsp;添加内容
                        </button>
                    </div>
                </div><!-- /.box-header -->
                <div class="box-body">
                    <table id="table" class="table table-bordered table-striped" width="100%">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>排序</th>
                            <th  width="200">标题</th>
                            <th>发布人</th>
                            <th>更新时间</th>
                            <th>状态</th>
                            <th>管理操作</th>
                        </tr>
                        </thead>
                    </table>
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div>
        <div class="col-xs-3">
            <div class="box">
                <div class="box-header">
                    <div class="box-title"><label>选择栏目</label></div>
                </div><!-- /.box-header -->
                <div class="box-body">
                    <div>
                        <span class="tree-icon tree-folder tree-folder-open"></span>
                        <a href="javascript:;" onclick="contentRightTreeRefresh()">刷新</a> |
                        <a href="javascript:;" onclick="contentRightTreeCollapseAll()">收起</a> |
                        <a href="javascript:;" onclick="contentRightTreeExpandAll()">展开</a>
                    </div>
                    <div>
                        <input type="hidden" id="catid" value="{$catid}"/>
                        <ul id="content_public_right_tree" class="easyui-tree"
                            data-options="url:'{:U('Content/public_right')}',animate:true,lines:true"></ul>
                    </div>
                    <script type="text/javascript">
                        $(".easyui-datebox").datebox();
                        //搜索
                        function contentNewsListDatagridSearch() {
                            contentMainLoad($('#content_newslist').attr('url'));
                        }

                        //格式化
                        $('#content_public_right_tree').tree({
                            onClick: function (node) {
                                if (!node.children || node.children.length == 0) {
                                    contentRightTreeTypeFunc(node.type, node.id);
                                }
                            },
                            formatter: function (node) {
                                var res = node.text;
                                if (!node.children || node.children.length == 0) {
                                    var typeList = eval('{:json_encode(C("CONTENT_CATEGORY_TYPE"))}');
                                    res = node.text + ' [' + typeList[node.type] + ']';
                                }
                                return res;
                            }
                        });
                        //刷新
                        function contentRightTreeRefresh() {
                            $('#content_public_right_tree').tree('reload');
                        }
                        //收起
                        function contentRightTreeCollapseAll() {
                            $('#content_public_right_tree').tree('collapseAll');
                        }
                        //展开
                        function contentRightTreeExpandAll() {
                            $('#content_public_right_tree').tree('expandAll');
                        }
                        if ( $("#catid").val() != 0) {
                            $("#addContentButton").removeClass('hide');
                        }
                        //类型对应的功能
                        function contentRightTreeTypeFunc(type, id) {
                            $("#catid").val(id);
                            if (id != 0) {
                                $("#addContentButton").removeClass('hide');
                            }
                            switch (type) {
                                //栏目
                                case '0':
                                    var url = "{:U('Content/newsList')}";
                                    url += url.indexOf('?') != -1 ? '&catid=' + id : '?catid=' + id;
                                    $('#content_newslist').attr('url', url);
                                    contentMainLoad(url, false);
                                    break;
                                //页面
                                case '1':
                                    var url = "{:U('Content/page_iframe')}";
                                    url += url.indexOf('?') != -1 ? '&catid=' + id : '?catid=' + id;
                                    $('#content_newslist').attr('url', url);
                                    loadContent(url);
                                    break;
                                //链接
                                case '2':
                                    return false;
                                    break;
                                default:
                                    $.messager.alert('提示信息', '未知的类型', 'error');
                            }
                        }
                        var datatable = $('#table').dataTable({
                            "autoWidth": true,
                            "searching": false,
                            "ordering": false,
                            "paging": true,
                            "serverSide": true,
                            "ajax": "{:U('Content/newsList',array('catid'=>$catid))}",
                            "columns": [
                                {"data": "id"},
                                {"data": "listorder"},
                                {"data": "title"},
                                {"data": "username"},
                                {"data": "inputtime"},
                                {"data": "status"},
                                {"data": "id"}
                            ],
                            "columnDefs": [
                                {
                                    "targets": 1,
                                    "render": function (data, type, row) {
                                        return '<input type="text" style="text-align:center;height: 20px" size="2" value="' + data + '" id="content_order_input' + row.id + '" onblur="contentOrder(' + row.id + ')">';
                                    }
                                },
                                {
                                    "targets": 5,
                                    "render": function (data, type, row) {
                                        var status = ["<label style='color:#ff473b'>未发布</label>", "<label style='color:#2eb5ff'>已发布</label>"];
                                        if (status[data]) {
                                            return status[data];
                                        } else {
                                            return "";
                                        }
                                    }
                                },
                                {
                                    "targets": 6,
                                    "render": function (data, type, row) {
                                        return '<a href="javascript:uptateContentItem(' + data + ')"><i class="fa fa-edit"></i>&nbsp;编辑</a>' +
                                                '&nbsp;&nbsp;&nbsp;<a href="javascript:deleteContentItem(' + data + ')"><i class="fa fa-trash-o"></i>&nbsp;删除</a>';
                                    }
                                }

                            ],
                            "language": lang
                        });
                        function contentMainLoad(url) {
                            url+= url.indexOf('?') != -1 ? '&':'?'
                            datatable.api().ajax.url(url + $('#content_newslist').serialize()).load();
                        }
                        //排序
                        function contentOrder(id){
                            $.post('{:U("Content/order_news")}', "order="+$('#content_order_input'+id).val()+"&id="+id, function(res){
                                if(!res.status){
                                    $.messager.alert('提示信息', res.info, 'error');
                                }else{
                                    $.messager.alert('提示信息', res.info, 'info');
                                    var url =$('#content_newslist').attr('url');
                                    var data = $('#content_newslist').serialize();
                                    url += url.indexOf('?') != -1 ? '&' + data : '?' + data;
                                    datatable.api().ajax.url($('#content_newslist').attr('url')).load();
                                }
                            }, 'json');
                        }

                        //添加
                        function contentNewsListAdd() {
                            var catid = $("#catid").val();
                            if (catid == 0) {
                                $.messager.error("请先选择栏目");
                                return;
                            }
                            var url = "{:U('Content/add_news_iframe')}";
                            url += url.indexOf('?') != -1 ? '&catid=' + catid : '?catid=' + catid;
                            loadContent(url);
                        }

                        function uptateContentItem(id) {
                            var url = "{:U('Content/add_news_iframe')}";
                            url += (url.indexOf('?') != -1 ? '&':'?')+"id="+id;
                            loadContent(url);
                        }

                        function deleteContentItem(id) {
                            $.messager.confirm('确认删除', '确认要删除内容？', function () {
                                $.ajax({
                                    url: "{:U('Content/delete_news')}",
                                    data: "ids[]=" + id,
                                    type:'post',
                                    success: function (json) {
                                        if (!json.status) {
                                            $.messager.error(json.info);
                                            return;
                                        } else {
                                            $.messager.msg(json.info);
                                        }
                                        var url =$('#content_newslist').attr('url');
                                        var data = $('#content_newslist').serialize();
                                        url += url.indexOf('?') != -1 ? '&' + data : '?' + data;
                                        datatable.api().ajax.url($('#content_newslist').attr('url')).load();
                                    }
                                });
                            });
                        }
                    </script>
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div>
    </div>
</section>
